<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Member;
use app\model\Config;
use app\model\Qrcode;
use app\model\Agent;
use app\model\AgentCode;

class AgentController extends Base
{
    public function amount()
    {
        $Membermob    = new Member;
        $memberinfo   = $Membermob->getUserByWechat();
        $data['base'] = $memberinfo;

        return $this->json(['data' => $data]);
    }

    public function poster()
    {
        $Membermob  = new Member;
        $Qrcode     = new Qrcode;
        $memberinfo = $Membermob->getUserByWechat();
        $Qrcodedata = $Qrcode->getUserQrcode($memberinfo['id'], 'pages/index/index');
        $config     = Config::getconfig();
        $agent      = Config::getconfig('agent');

        $data['sitename'] = $config['sys_title'];
        $data['poster']   = toimg($agent['poster']);
        $data['nickName'] = $memberinfo['nickname'];
        $data['qrcode']   = $Qrcodedata;

        return $this->json(['data' => $data]);
    }

    public function check()
    {
        $uid  = UID();
        $data = Agent::where(['uid' => $uid, 'status' => 1])->find();
        if (!empty($data)) {
            $data = $data->toArray();
        }

        if (input('get.v') == Config::getconfig()['program_version']) {
            $data['is_audit'] = 1;
        } else {
            $data['is_audit'] = 0;
        }
        $data['agent_code'] = AgentCode::getagent_code($uid, weid());

        return $this->json(['msg' => 'ok', 'data' => $data]);
    }

    public function upcode()
    {
        $agent_code = input('post.agent_code');

        if (!empty($agent_code)) {
            $agent_code = AgentCode::upcode(UID(), $agent_code, 0, weid());
        }
        if (empty($agent_code)) {
            return $this->json(['errno' => 1, 'message' => '这个邀请码已有人使，请换别的！', 'data' => '']);
        } else {
            return $this->json(['data' => $agent_code]);
        }
    }

    /**
     * @menu 审核列表
     * @Author:男人的坏
     * @Date:2024-04-05 14:56
     */
    public function audit_index()
    {
        $status = input('status', 0);
        $data   = Agent::where(['weid' => weid(), 'status' => $status])->order('id desc')->paginate()->each(function ($query){
            $info = AgentCode::where(['uid'=>$query->uid])->findOrEmpty();
            $query->pid_code = '';
            if(!$info->isEmpty()){
                $query->pid_code = $info->agent_code;
            }

        });
        return $this->json(['data' => $data]);
    }

    /**
     * @menu 审核
     * @Author:男人的坏
     * @Date:2024-04-05 16:21
     */

    public function audit_update()
    {
        $ids    = input('ids');
        $status = input('status', 0);
        if (!$ids || !in_array($status, [0, 1])) {
            return $this->json(['errno' => 0, 'message' => '参数错误！', 'data' => '']);
        }
        $info = Agent::where(['weid' => weid(), 'id' => $ids])->findOrEmpty();
        if ($info->isEmpty()) {
            return $this->json(['errno' => 0, 'message' => '数据不存在！', 'data' => '']);
        }
        $info->status = $status;
        $info->save();
        return $this->json(['msg' => 'ok']);
    }
}
